<?php
apf_require_controller("Abstract");
apf_require_class("Util_ApioAuth");
apf_require_class("Bll_Jx_Setting");
apf_require_class("Util_ApioAuth");
class Ajax_structureTreeController extends AbstractController{
    public function handle_request() {
        $params=APF::get_instance()->get_request()->get_parameters();
        if($params['checkbox'])
        {
          $bll=new Bll_Jx_Setting();
          $ids=$params['ids'];
          $user=$bll->get_user_info_by_id($params['user_id']);
          if($ids)
           foreach ($ids as $id)
         {
             if($id!="undefined")
           {
              $member=$bll->get_member_by_code($id);
              $s=$bll->set_virtual_leader($member[0]['user_id'], $user[0]['user_id'],$user[0]['code']);
           }
         }
        }
        $jsondata=$this->get_subordinate_infor($params['code']);
        $subordinate=json_decode($jsondata,true);
        if($subordinate[0]['subordinate'])
        $this->json_encode(count($subordinate[0]['subordinate']),$subordinate[0]['subordinate']);
        else 
        $this->json_encode(0,"none");
    }
    public function get_subordinate_infor($code){
        return Util_ApioAuth::get_user_info_by_api($code);
    }
    public function get_data_format($data){
                $tmp=json_decode($data,true);
                $underling=array();
        foreach ($tmp[0]['subordinate'] as $sub){
                if($sub!=null)
                {
                 array_push($underling,array("name"=>$sub['name'],"code"=>$sub['code']));
                }
        }
        return $underling;
    }
    public function json_encode($size,$res){
        $result = array(
                'size'=>$size,
                'data'=>$res
        );
        echo json_encode($result);
        exit();
    }
}